package com.hhc.service.impl;

import com.hhc.dto.ExamArrangementDTO;
import com.hhc.entity.ExamArrangementDO;
import com.hhc.query.ExamArrangementQuery;
import com.hhc.repository.CourseInfoRepository;
import com.hhc.repository.ExamArrangementRepository;
import com.hhc.service.ExamArrangementService;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 考试安排业务逻辑层实现
 * </p>
 * @author pc
 */
@Slf4j
@Service
public class ExamArrangementServiceImpl implements ExamArrangementService {

    @Autowired
    private ExamArrangementRepository examArrangementRepository;

    @Autowired
    private CourseInfoRepository courseInfoRepository;

    @Transactional
    @Override
    public ExamArrangementDTO addExamArrangement(ExamArrangementDTO examArrangementDTO) {
        if (!courseInfoRepository.existsById(examArrangementDTO.getCourseId())) {
            throw new IllegalArgumentException("课程信息不存在");
        }

        ExamArrangementDO examArrangementDO = new ExamArrangementDO();
        BeanUtils.copyProperties(examArrangementDTO, examArrangementDO);

        examArrangementRepository.save(examArrangementDO);
        return examArrangementDTO;
    }

    @Transactional
    @Override
    public ExamArrangementDTO updateExamArrangement(ExamArrangementDTO examArrangementDTO) {
        if (!examArrangementRepository.existsById(examArrangementDTO.getId())) {
            throw new IllegalArgumentException("考试安排信息不存在");
        }

        ExamArrangementDO examArrangementDO = examArrangementRepository.findById(examArrangementDTO.getId()).orElseThrow(() -> new IllegalArgumentException("考试安排信息不存在"));
        BeanUtils.copyProperties(examArrangementDTO, examArrangementDO);

        examArrangementRepository.save(examArrangementDO);
        return examArrangementDTO;
    }

    @Transactional
    @Override
    public void deleteExamArrangement(Long id) {
        if (!examArrangementRepository.existsById(id)) {
            throw new IllegalArgumentException("考试安排信息不存在");
        }

        examArrangementRepository.deleteById(id);
    }

    @Override
    public Page<ExamArrangementDO> listExamArrangements(ExamArrangementQuery query) {
        Pageable pageable = PageRequest.of(0, 10); // 默认分页
        return examArrangementRepository.findAll(pageable);
    }

    @Override
    public ExamArrangementDTO getExamArrangementById(Long id) {
        Optional<ExamArrangementDO> optionalExamArrangementDO = examArrangementRepository.findById(id);
        if (!optionalExamArrangementDO.isPresent()) {
            throw new IllegalArgumentException("考试安排信息不存在");
        }

        ExamArrangementDO examArrangementDO = optionalExamArrangementDO.get();
        ExamArrangementDTO examArrangementDTO = new ExamArrangementDTO();
        BeanUtils.copyProperties(examArrangementDO, examArrangementDTO);

        return examArrangementDTO;
    }
}